package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

/**
 *资费计划基本信息通信套包类型与服务信息套包类型联动展示 固定共享池流量（MB/期限） 账户费用（元/期限）
 * “套包类型”选择“标准套包”且资费套包类型选择【预付固定共享表 】【月付固定共享】时展示
 */
public class PricingPlanServInfoLinkageAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    IOperateSceneInstService operateSceneInstService;

    @Override
    public void onShow(KiteAttr attr, KiteInst inst, AttrData data) {
        String fieldName = attr.getFieldName();
        KiteProdInst prodInst = querySceneInstService.getProdInstByFuncProdInstId(data.getCustId(),inst.getInstId());
        if(KiteObjectUtils.isEmpty(prodInst)){
            return;
        }
        List<KiteProdInstFunc> funcs = querySceneInstService.listProdInstFuncsBySceneInstId(prodInst.getProdInstId(),data.getSceneInstId());
        if(KiteListUtils.isNotEmpty(funcs)){
            String CommunicationPacketType = "";
            for(KiteProdInstFunc func : funcs){
                //通信套包类型
                KiteAttr kiteAttr = func.getAttr("200003697");
                if (Objects.isNull(kiteAttr)) {
                    // 资费套包类型
                    kiteAttr = func.getAttr("200004179");
                }
                if(!KiteObjectUtils.isEmpty(kiteAttr)){
                    CommunicationPacketType = kiteAttr.getValue();
                    break;
                }
            }
            //通信套包类型
            String packageType = "";
            KiteAttr kiteAttr = inst.getAttr("200003708");
            if(!KiteObjectUtils.isEmpty(kiteAttr)){
                packageType = kiteAttr.getValue();
            }
            if (KiteStringUtils.equalsAny(fieldName, "200003703", "200003705")) {
                if (!(KiteStringUtils.equals("100007469", CommunicationPacketType) || KiteStringUtils.equals("100007470", CommunicationPacketType)) && KiteStringUtils.equals("100006435", packageType)) {
                    attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    attr.setIsNull(KeyConsts.IFTRUE_F);
                } else {
                    attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                    attr.setIsNull(KeyConsts.IFTRUE_T);
                }
            } else {
                if ((KiteStringUtils.equals("100007469", CommunicationPacketType) || KiteStringUtils.equals("100007470", CommunicationPacketType)) && KiteStringUtils.equals("100006435", packageType)) {
                    attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                    attr.setIsNull(KeyConsts.IFTRUE_F);
                } else {
                    attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                    attr.setIsNull(KeyConsts.IFTRUE_T);
                }
            }
        }
    }
}
